import Vue from "vue";
import Vuex from "vuex";

import product from "@/store/product";
import cart from "@/store/cart";

import timeTrave from "./plugins/timeTravel";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);

export default new Vuex.Store({
  strict: true,
  state: {
    user: "lbw",
    num: 666,
    products: [],
    msg: "神奇",
  },
  mutations: {
    INCREMENT_NUM(state, payload) {
      state.num = state.num + payload;
    },
    DECREMENT_NUM(state, payload) {
      state.num = state.num - payload;
    },
    SET_PRODUCT(state, payload) {
      state.products = payload;
    },
  },
  actions: {
    GET_PRODUCT(context, payload) {
      fetch("http://chst.vip/data/index.json")
        .then((body) => body.json())
        .then((res) => {
          context.commit("SET_PRODUCT", res.data);
        });
    },
  },
  getters: {
    reverseMsg(state) {
      return state.msg.split("").reverse("").join("");
    },
  },
  modules: {
    product,
    cart,
  },
  plugins: [
    // timeTrave,
    createPersistedState({
      storage: window.sessionStorage,
      key: "2204",
      paths: ["products", "user"],
    }),
  ],
});
